import pymysql.cursors

def getConnection():
    config = {
    'host': '115.159.219.219',
    'port': 3306,
    'user': 'root',
    'password': 'wdm8080',
    'db': 'wechat',
    'charset': 'utf8mb4',
    'cursorclass': pymysql.cursors.DictCursor,
    }

    # Connect to the database
    connection = pymysql.connect(**config)
    return connection

def insert(sql):
    # 执行sql语句
    try:
        connection = getConnection()
        with connection.cursor() as cursor:
            # 执行sql语句，插入记录
            cursor.execute(sql);
        # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
        connection.commit()
    finally:
        connection.close();


def update(sql):
    # 执行sql语句
    try:
        connection = getConnection()
        with connection.cursor() as cursor:
            # 执行sql语句，进行查询
            cursor.execute(sql,)
        # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
        connection.commit()
    finally:
        connection.close();


def select(sql):
    # 执行sql语句
    result = None
    try:
        connection = getConnection()
        with connection.cursor() as cursor:
            # 执行sql语句，进行查询
            cursor.execute(sql,)
            # 获取查询结果
            result = cursor.fetchone()

        # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
        connection.commit()
    finally:
        connection.close();
    return result

#update("update wechat.login set send_content = '测试测试测试' where login_id = 'b137ddca-8c84-11e7-ad5c-2c6e85cda469'")
